<?php
/**
* Author: Porlock
* Link: www.porlockz.com
* Date: 2018-04-22 22:35:18
* Last Modified time: 2018-04-22 22:35:22
*/
require_once ('includes/lib/func_register.php');
$home_url = '/afctf/index.php';
msg_display('注册已结束，预祝各位参赛选手都能取得好成绩！','error',$home_url);
if(@$_GET['action'] == 'register'){
    echo <<<EOT
<!--注册-->
    <link href="css/login.css" rel="stylesheet" type="text/css" media="all"/>
    <div class="register-form">
        <div class="first-login">
            <div class="title">
                <h2>选手注册</h2>
            </div>
            <div class="content-body">
                <form action="/afctf/members/register.php?action=deal" method="post">
                    <div class="input-wrapper">
                        <input type="text" id="re_uid" name="re_uid" autofocus="autofocus" required="required" placeholder="请输入你的学号（10位）" maxlength="10">
                    </div>
                    <div class="input-wrapper">
                        <input type="text" name="re_name" required="required" placeholder="请输入你的姓名" maxlength="8">
                    </div>
                    <div class="input-wrapper">
                        <input type="text" name="re_nickname" required="required" placeholder="请输入你的昵称" maxlength="14">
                    </div>
                    <div class="input-wrapper">
                        <input type="password" name="re_pw1" required="required" placeholder="请输入你的密码（6~16位）" maxlength="16">
                    </div>
                    <div class="input-wrapper">
                        <input type="password" name="re_pw2" required="required" placeholder="请输入你的密码（6~16位）" maxlength="16">
                    </div>
                    <div class="input-wrapper">
                        <input type="email" name="re_email" required="required" placeholder="请输入你的邮箱" maxlength="50">
                    </div>
                    <div class="checkcode-wrapper input-wrapper">
                        <input type="text" id="checkcode-input" name="re_verify" required="required" placeholder="请输入验证码" maxlength="5">
                        <img id="checkcode-img" src="/afctf/includes/lib/checkcode.php" onclick="this.src='/afctf/includes/lib/checkcode.php?action=refresh'"/>
                    </div>
                    <div>
                        <input class="submit" type="submit"  value="注册">
                    </div>
                </form>
            </div>
        </div>
    </div>

EOT;

}
/*
 *  1.接收数据
 *  2.检查是否有空数据
 *  3.检查学号是否为数字
 *  4.检查邮箱地址是否正确
 *  5.检查密码是否一致
 *  6.随机生成盐值并加密密码获得hash值
 *  7.注册
 *  8.跳转登录界面
 */
elseif (@$_GET['action'] == 'deal'){
    require_once ('../init.php');
    require_once ('includes/lib/func_register.php');
    require_once ('includes/views/jump_header.php');
    session_start();
    $verify = $_POST['re_verify'];
    $code = $_SESSION['code'];
    // 使用完验证码之后清除
    unset($_SESSION['code']);
    $register_url = '/afctf/index.php?action=register';
    $login_url = '/afctf/index.php?action=login';
    $home_url = '/afctf/index.php';

    $uid = $_POST['re_uid'];
    $name = $_POST['re_name'];
    $nickname = $_POST['re_nickname'];
    $email = $_POST['re_email'];
    $password1 = $_POST['re_pw1'];
    $password2 = $_POST['re_pw2'];
    // 判断是否填写了验证码，防止批量注册
    if(!isset($verify)){
        msg_display('请填写验证码','error',$register_url);
    }
    // 检查验证码是否正确
    if(strnatcasecmp($verify,$code)){
        msg_display('验证码错误','error',$register_url);
    }
    // 检查是否有空数据
    if (!filled_out($_POST)){
        msg_display('表单有空数据','error',$register_url);
    }
    // 检查学号
    $numStatus = check_num($uid);
    if ($numStatus === 4) {
        msg_display('学号必须为10位','error',$register_url);
    }elseif($numStatus === 3){
        msg_display('学号必须都为数字','error',$register_url);
    }elseif ($numStatus === 2) {
        msg_display('抱歉，本次比赛只面向16,17级同学','error',$register_url);
    }
    // 检查姓名是否为汉字
    if (!check_chinese($name)){
        msg_display('姓名必须为汉字','error',$register_url);
    }
    // 检查姓名长度
    if (!check_strlength($name,8)){
        msg_display('姓名长度超出范围','error',$register_url);
    }
    // 检查昵称字符
    if (!check_nickname($nickname)){
        msg_display('昵称只能由汉字、英文、数字和 !@#￥%^&*()._ 组成','error',$register_url);
    }
    // 检查昵称长度
    if (!check_strlength($nickname,14)){
        msg_display('昵称长度最多为14位','error',$register_url);
    }
    // 检查密码
    $passwdStatus = check_password($password1,$password2);
    if ($passwdStatus === 2){
        msg_display('两次填写的密码不一致','error',$register_url);
    }elseif ($passwdStatus === 3) {
        msg_display('密码的长度需要在6~16之间','error',$register_url);
    }elseif ($passwdStatus === 4) {
        msg_display('密码只能使用英文字母数字以及!@#$%^&*()_等字符','error',$register_url);
    }
    // 检查邮箱长度
    if (!check_strlength($email,50)){
        msg_display('邮箱长度超出范围','error',$register_url);
    }
    //  检查邮箱地址
    if (!valid_email($email)){
        msg_display('邮件格式错误','error',$register_url);
    }
    //  随机生成盐值并加密密码获得hash值
    $salt = getrandstr(32);
    $password = hash('sha256',$password1.$salt);
    //  注册
    try {
        register($dbh,$uid,$name,$nickname,$email,$salt,$password);
        // 跳转至登录界面
        msg_display('注册成功','success',$login_url);
    } catch(PDOException $exception) {
        $errorMessage = $exception->getMessage();
        preg_match('/\'[a-zA-Z]+\'$/',$errorMessage,$match);
        if($match[0] == "'PRIMARY'"){
            msg_display('该学号已被注册','error',$register_url);
        }
        if($match[0] == "'name'"){
            msg_display('该姓名已被注册','error',$register_url);
        }
        if($match[0] == "'nickname'"){
            msg_display('该昵称已被注册','error',$register_url);
        }
        if($match[0] == "'email'"){
            msg_display('该邮箱已被注册','error',$register_url);
        }
    } catch (Exception $e) {
        msg_display('错误代码111，请联系管理员','error',$register_url);
    }
}
